import {request} from "./request";

const TOKEN_KEY = 'token'



function login() {
    return new Promise((resolve, reject) => {
        wx.login({
            success(loginRes) {
                const code = loginRes.code
                request.request({
                    url: '/api/login/index', // 替换为你的登录接口
                    method: 'POST',
                    data: { code },
                    success(res) {
                        if (res.data && res.data.token) {
                            wx.setStorageSync(TOKEN_KEY, res.data.token)
                            resolve(res.data.token)
                        } else {
                            reject('登录失败')
                        }
                    },
                    fail: reject
                })
            },
            fail: reject
        })
    })
}

function getToken() {
    return  getApp().globalData.accessToken || ''
}


export { getToken, login }

